package usf.gwt.ui.bootstrap.datetimepicker.client;

import usf.gwt.ui.bootstrap.client.core.BootstrapContainer;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;

public class YearCalendar extends BootstrapContainer {
	
	protected static final int MONTHS = 12;
	protected static final int DEFAULT_FIRST_MONTHS = 0;
	public static final String DEFAULT_CALENDAR_MONTH_STYLE = "calendar-month";
	
	protected int diplayedFirstMonth = 0;
	protected int diplayedYear = 0;
	
	public YearCalendar() {
		super();
	}

	@Override
	protected Element initWidget() {
		return Document.get().createDivElement();
	}
	
	public void setDisplayedYear(int year) {
		setDisplayedYear(year, DEFAULT_FIRST_MONTHS);
	}
	
	public void setDisplayedYear(int year, int first) {
		clear();
		this.diplayedYear = year;
		this.diplayedFirstMonth = first % (MONTHS + 1); int i;
		for(i=this.diplayedFirstMonth; i<MONTHS; i++) add(createMonthCalendar(i, year));
		for(i=0; i<this.diplayedFirstMonth; i++) add(createMonthCalendar(i, year+1));
	}
	
	public void appendStyleName(String styleName, int month, int... days){
		month = (MONTHS - diplayedFirstMonth + month) % MONTHS;
		MonthCalendar c = (MonthCalendar)getWidget(month);
		for(int i=0; i<days.length; i++) c.appendStyleName(styleName, days[i]);
	}
	
	protected static MonthCalendar createMonthCalendar(int month, int year){
		MonthCalendar c = new MonthCalendar(month, year);
		c.getElement().setClassName(DEFAULT_CALENDAR_MONTH_STYLE);
		return c;
	}
	
	public int getDiplayedFirstMonth() {
		return diplayedFirstMonth;
	}
	public int getDiplayedYear() {
		return diplayedYear;
	}
	
	public void clearStyle() {
		for(int i=0; i<getWidgetCount(); i++){
			MonthCalendar c = (MonthCalendar)getWidget(i);
			c.clearStyle();
		}
	}
}
